<?php

namespace app\sysuser\controller\student;

use app\common\controller\SysBackend;
use app\common\model\banxi\student\StudentCourse as StudentCourseModel;
use think\db\exception\PDOException;
use think\exception\ValidateException;
use think\facade\Validate;
use think\validate\ValidateRule;


/**
 * 学员
 */
class StudentCourse extends SysBackend
{
    protected array $noNeedLogin = [];

    protected array $noNeedPermission = ['*'];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new StudentCourseModel();
    }


    /**
     * 分页查询
     */
    public function listPage(): void
    {
        $param = $this->request->param(['index', 'limit', 'orderBy', "query",]);
        $where = get_query($param);
        $pageParams = get_page_params($param);
        $list = $this->model->getListPage($where, $pageParams['page'], $pageParams['limit']);
        $this->ok('操作成功', result_row($list));
    }


    /**
     * 开通课程
     * @return void
     * @throws \think\db\exception\DbException
     */
    public function openCourse(): void
    {
        $param = $this->request->param(["studentId" => 1, 'courseIds' => []]);
        try {
            $validate = Validate::rule([
                'studentId' => ValidateRule::isRequire(null, '请选择开通教程的学生'),
                'courseIds' => ValidateRule::isRequire(null, '请选择教程'),
            ]);
            if (!$validate->check($param)) throw new ValidateException($validate->getError());
            $this->model->openCourse($param['studentId'], $param['courseIds']);
        } catch (ValidateException $exception) {
            $this->fail($exception->getMessage());
        }
        $this->ok();
    }

    /**
     * 查询
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function get(): void
    {
        $param = $this->request->param(["query" => []]);
        if (empty($param['query'])) {
            $this->fail('请传递你要查询的记录');
        }
        $where = get_query($param);
        $info = $this->model->getInfo($where);
        if (empty($info)) {
            $this->fail('记录不存在');
        }
        $this->ok('操作成功', $info);
    }


    /**
     * 新增或修改
     * @return void
     */
    public function saveOrUpdate(): void
    {
        $param = $this->request->param();
        if (empty($param)) {
            $this->ok('请传入参数');
        }
        try {
            $this->model->saveOrUpdate($param);
        } catch (ValidateException|PDOException|\Exception $exception) {
            $this->fail($exception->getMessage());
        }
        $this->ok('操作成功', [], self::NOTIFICATION);
    }

    public function del()
    {
        $param = $this->request->param(['ids' => []]);
        try {
            $validate = Validate::rule([
                'ids' => ValidateRule::isRequire(null, '请选择你要删除的记录'),
            ]);
            if (!$validate->check($param)) throw new ValidateException($validate->getError());
            $this->model->del($param['ids']);
        } catch (ValidateException $exception) {
            $this->fail($exception->getMessage());
        }
        $this->ok('操作成功', [], self::NOTIFICATION);
    }
}